class AdminsController < ApplicationController

  before_filter :authenticate
  before_filter :ensure_administrator

  # 
  # Users
  #

  def users
    params[:page] ? page = params[:page] : page = 1
    @users = User.paginate(:page => page, :order => "created_at ASC")
  end
  
  def edit_user
    @edited_user = User.find(params[:id])
  end
  
  def update_user
    @edited_user = User.find(params[:id])
    @edited_user.is_administrator = true if params[:edited_user][:id_administrator]
    if @edited_user.update_attributes(params[:edited_user])
      flash[:notice] = 'User was successfully saved.'
      log_event("User updated", @edited_user.id)
      redirect_to :action => :users
    else
      render :action => :edit_user
    end
  end
  
  def new_user
    @edited_user = User.new
  end
  
  def create_user 
    @edited_user = User.new(params[:edited_user])
    @edited_user.is_administrator = true if params[:edited_user][:id_administrator]
    if @edited_user.save
      flash[:notice] = 'User was successfully created.'
      log_event("User created", @edited_user.id)
      redirect_to :action => :users
    else
      render :action => :new_user 
    end
  end
  
  def destroy_user
    user = User.destroy(params[:id])
    log_event("User destroyed", user.id)
    redirect_to :action => :users
  end
  
  def new_password
    @edited_user = User.find(params[:id])
    @edited_user.password = nil
  end
  
  def change_password
    @edited_user = User.find(params[:id])
    if @edited_user.update_attributes(params[:edited_user])
      @edited_user.encrypt_password
      @edited_user.save
      flash[:notice] = 'User was successfully saved.'
      log_event("Password changed", @edited_user.id)
      redirect_to :action => :users
    else
      render :action => :new_password
    end
  end
  
  #
  # Posts
  #
  
  def posts
    params[:page] ? page = params[:page] : page = 1
    @posts = Post.paginate(:page => page, :order => "created_at ASC")
  end
  
  def destroy_post
    post = Post.destroy(params[:id])
    log_event("Post destroyed", post.id)
    redirect_to :action => :posts
  end
  
  def new_post
    @post = Post.new 
    @post.comments_disabled_after = APP_SETTINGS[:disable_post_comments_after]
  end
  
  def create_post
    
    @post = Post.new(params[:post])
    @post.user_id = @user.id

    unless params[:asset].blank? or params[:asset][:uploaded_file].blank?
      @asset = Asset.new(params[:asset])    
      if @asset.save
        log_event("Asset created", @asset.id)
        @post.asset_id = @asset.id
      else
        log_event("Asset can not be created", @user.id)
        @post.errors.add_to_base("Can not attach the provided image to this post.")
        render :action => "new_post"
        return
      end
    end
    
    if @post.save
      flash[:notice] = 'Post was successfully created.'
      log_event("Post created", @post.id)
      redirect_to :action => :posts
    else
      render :action => :new_post 
    end

  end
  
  def edit_post
    @post = Post.find(params[:id])
  end
  
  def update_post
    @post = Post.find(params[:id])
    
    unless params[:asset].blank? or params[:asset][:uploaded_file].blank?
      @asset = Asset.new(params[:asset])    
      if @asset.save
        log_event("Asset created", @asset.id)
        @post.asset_id = @asset.id
      else
        log_event("Asset can not be created", @user.id)
        @post.errors.add_to_base("Can not attach the provided image to this post.")
        render :action => "edit_post"
        return
      end
    end
    
    if @post.update_attributes(params[:post])
      flash[:notice] = 'Post was successfully saved.'
      log_event("Post updated", @post.id)
      redirect_to :action => :posts
    else
      render :action => :edit_post
    end
  end
  
  def lock_post
    @post = Post.find(params[:id])
    @post.comments_enabled = false
    @post.save
    log_event("Post locked", @post.id)
    redirect_to :action => :posts
  end
  
  #
  # Comments
  #
  
  def comments
    params[:page] ? page = params[:page] : page = 1
    @comments = Comment.paginate(:page => page, :order => "created_at ASC")
  end
  
  def destroy_comment
    comment = Comment.destroy(params[:id])
    log_event("Comment destroyed", comment.id)
    redirect_to :action => :comments
  end
  
  #
  # Pages
  #
  
  def pages
    params[:page] ? page = params[:page] : page = 1
    @pages = Page.paginate(:page => page, :order => "created_at ASC")
  end
  
  def edit_page
    @page = Page.find(params[:id])
  end
  
  def update_page
    @page = Page.find(params[:id])
    if @page.update_attributes(params[:page])
      flash[:notice] = 'Page was successfully saved.'
      log_event("Page updated", @page.id)
      redirect_to :action => :pages
    else
      render :action => :edit_page
    end
  end
  
  def new_page
    @page = Page.new   
  end
  
  def create_page
    @page = Page.new(params[:page])
    @page.user_id = @user.id
    if @page.save
      flash[:notice] = 'Page was successfully created.'
      log_event("Page created", @page.id)
      redirect_to :action => :pages
    else
      render :action => :new_page 
    end
  end
  
  def destroy_page
    page = Page.destroy(params[:id])
    log_event("Page destroyed", page.id)
    redirect_to :action => :pages
  end
  
  #
  # Logs
  #
  
  def logs
    params[:page] ? page = params[:page] : page = 1
    @logs = Log.paginate(:page => page, :order => "created_at DESC")
  end
  
  #
  # Tags
  #
  
  def tags
    @tags = Tag.find(:all)
  end
  
  def new_tag
    @tag = Tag.new   
  end
  
  def create_tag
    @tag = Tag.new(params[:tag])
    if @tag.save
      flash[:notice] = 'Tag was successfully created.'
      log_event("Tag created", @tag.id)
      redirect_to :action => :tags
    else
      render :action => :new_tag
    end
  end
  
  def destroy_tag
    taggings = Tagging.find_by_tag_id(params[:id])
    if taggings == nil 
      tag = Tag.destroy(params[:id]) 
      log_event("Tag destroyed", tag.id)
    else
      log_event("Can't destroy tag that is in use", nil)
    end
    redirect_to :action => :tags
  end
  
end
